<template>
    <div class="layout">
        <div class="pre">
            <div class="pre_title">
                <span class="title-text">点赞时段</span>
                <span v-auth="'GIVE_EXP_BOARD'" class="export" @click="exportImg1(myRef_one, '点赞时段')">导出</span>
            </div>
            <div v-show="!isNoMessage1" id="echarts" ref="myRef_one" class="min"></div>
            <div v-show="isNoMessage1" class="no_data">
                <img class="no_img" src="@/assets/noData.png" alt="" />
                <div class="emty_title">暂无数据</div>
            </div>
        </div>
        <div class="pre">
            <div class="pre_title">
                <span class="title-text">评论时段</span>
                <span v-auth="'COMMENT_EXP_BOARD'" class="export" @click="exportImg2(myRef_two, '评论时段')">导出</span>
            </div>
            <div v-show="isNoMessage2" id="echarts" ref="myRef_two" class="min"></div>
            <div v-show="!isNoMessage2" class="no_data">
                <img class="no_img" src="@/assets/noData.png" alt="" />
                <div class="emty_title">暂无数据</div>
            </div>
        </div>
    </div>
</template>
<script lang="ts" setup>
import { onMounted, reactive, ref, inject, watch, nextTick } from 'vue';
import { export2Img } from '@/utils/utils';
import { ElMessage } from 'element-plus';
const echarts = inject('ec');
const myRef_one = ref(null);
const isNoMessage1 = ref(false);
const myRef_two = ref(null);
const isNoMessage2 = ref(false);
const props = defineProps(['upData', 'commentData']);
//导出图表
const exportImg1 = (value: any, name: any) => {
    if (name == '点赞时段' && isNoMessage1.value) {
        ElMessage.error('暂无数据可导出');
        return;
    } else {
        export2Img(value, name);
    }
};
const exportImg2 = (value: any, name: any) => {
    if (name == '评论时段' && isNoMessage2.value) {
        ElMessage.error('暂无数据可导出');
        return;
    } else {
        export2Img(value, name);
    }
};
const getChart = (data: any) => {
    if (data.length == 0) {
        isNoMessage1.value = true;
        return;
    } else {
        isNoMessage1.value = false;
    }
    const myChart_one = echarts.init(myRef_one.value);
    myChart_one.setOption({
        tooltip: {
            trigger: 'item'
        },
        series: [
            {
                type: 'pie',
                data: data,
                label: {
                    normal: {
                        show: true,
                        formatter: '{b} {d}%' //自定义显示格式(b:name, c:value, d:百分比)
                    }
                },
                itemStyle: {
                    normal: {
                        label: {
                            show: true,
                            formatter: function (val: any) {
                                const echartsData = data;
                                let total = 0;
                                let tarValue = 0;
                                for (let i = 0, l = echartsData.length; i < l; i++) {
                                    total += Number(echartsData[i].value);
                                    if (echartsData[i].name == val.name) {
                                        tarValue = echartsData[i].value;
                                    }
                                }
                            }
                        },
                        labelLine: { show: true }
                    }
                },
                color: ['#ff0000', '#ff8364', '#ffb988', '#fde860', '#ffbe49', '#b50000'],
                radius: '50%'
            }
        ]
    });
    nextTick(() => {
        myChart_one.resize();
    });
    window.onresize = function () {
        myChart_one.resize();
    };
};
const getChart2 = (data: any) => {
    if (data.length == 0) {
        isNoMessage2.value = false;
        return;
    } else {
        isNoMessage2.value = true;
    }
    const myChart_two = echarts.init(myRef_two.value);
    myChart_two.setOption({
        tooltip: {
            trigger: 'item'
        },
        series: [
            {
                type: 'pie',
                data: data,
                itemStyle: {
                    normal: {
                        label: {
                            show: true,
                            formatter: function (val: any) {
                                const echartsData = data;
                                let total = 0;
                                let tarValue = 0;
                                for (let i = 0, l = echartsData.length; i < l; i++) {
                                    total += Number(echartsData[i].value);
                                    if (echartsData[i].name == val.name) {
                                        tarValue = echartsData[i].value;
                                    }
                                }
                                const p = ((tarValue / total) * 100).toFixed(2);
                                return val.name + ' ' + ' ' + p + '%';
                            }
                        },
                        labelLine: { show: true }
                    }
                },
                color: ['#4cb050', '#259888', '#fcc108', '#feeb3c', '#cddc39', '#8bc24a'],
                radius: '50%'
            }
        ]
    });
    nextTick(() => {
        myChart_two.resize();
    });
    window.onresize = function () {
        myChart_two.resize();
    };
};
watch(
    () => props.upData,
    val => {
        getChart(val);
    }
);
watch(
    () => props.commentData,
    val => {
        nextTick(() => {
            getChart2(val);
        });
    }
);
</script>
<style lang="scss" scoped>
.layout {
    width: 100%;
    margin-top: 16px;
    display: flex;
    justify-content: space-between;

    .pre {
        width: 49.5%;
        height: 546px;
        box-sizing: border-box;
        background: #fff;
        border: 1px solid #eaeaea;

        &_title {
            padding: 16px;
            border-bottom: 1px solid #eaeaea;
            margin: 0 auto;
            display: flex;
            justify-content: space-between;

            .export {
                display: inline-block;
                width: 64px;
                height: 36px;
                text-align: center;
                line-height: 36px;
                background: #fff;
                border-radius: 4px;
                border: 1px solid #eaeaea;
                cursor: pointer;
            }
        }

        .title-text {
            line-height: 38px;
        }

        .min {
            width: 39vw;
            height: 400px;
        }
    }

    .no_data {
        height: 500px;
        display: flex;
        justify-content: center;
        align-items: center;
        flex-direction: column;

        .no_img {
            margin-left: 35px;
        }

        .emty_title {
            color: #909399;
            margin: 0;
        }
    }
}
</style>
